package com.xie.smfs.entity;

import com.xie.smfs.item.RedGhostCandleItem;
import com.xie.smfs.registry.ModEffects;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FogGhostEntity extends GhostEntity {

    @Override
    protected void applyDefaultEffects(PlayerEntity player) {
        // 应用自定义浓雾效果0级
        player.addStatusEffect(new StatusEffectInstance(ModEffects.THICK_FOG, 20, 0, false, false));
    }
    // 跟踪玩家移动开始时间 (UUID -> 开始移动的游戏刻数)
    private final Map<UUID, Long> playerMoveStartTicks = new HashMap<>();
    // 跟踪玩家上一时刻位置 (UUID -> 位置)
    private final Map<UUID, Vec3d> playerLastPositions = new HashMap<>();
    // 移动检测阈值 (方块)
    private static final double MOVEMENT_THRESHOLD = 0.1;
    // 触发攻击的持续移动时间 (刻) 3秒 = 60刻
    private static final int ATTACK_TRIGGER_TICKS = 60;

    public FogGhostEntity(EntityType<? extends GhostEntity> entityType, World world) {
        super(entityType, world, true, 3, 32.0); // 启用鬼域，等级1，半径15
        this.ghostLevel = 2; // 设置鬼等级
    }

    @Override
    protected boolean shouldAttackPlayer(PlayerEntity player) {
        if (isSuppressed() || isDeadlocked() || RedGhostCandleItem.isHoldingCandle(player)) {
            resetPlayerTracking(player);
            return false;
        }

        UUID playerId = player.getUuid();
        Vec3d currentPos = player.getPos();
        Vec3d lastPos = playerLastPositions.getOrDefault(playerId, currentPos);
        long currentTick = this.getWorld().getTime();

        // 计算移动距离
        double distanceMoved = currentPos.distanceTo(lastPos);
        boolean isMoving = distanceMoved > MOVEMENT_THRESHOLD;

        // 更新玩家位置记录
        playerLastPositions.put(playerId, currentPos);

        if (isMoving) {
            // 玩家正在移动
            if (!playerMoveStartTicks.containsKey(playerId)) {
                // 开始记录移动时间
                playerMoveStartTicks.put(playerId, currentTick);
            } else {
                // 计算持续移动时间
                long moveDuration = currentTick - playerMoveStartTicks.get(playerId);
                return moveDuration >= ATTACK_TRIGGER_TICKS;
            }
        } else {
            // 玩家停止移动，重置计时器
            if (playerMoveStartTicks.containsKey(playerId)) {
                resetPlayerTracking(player);
            }
        }
        return false;
    }

    private void resetPlayerTracking(PlayerEntity player) {
        UUID playerId = player.getUuid();
        playerMoveStartTicks.remove(playerId);
        playerLastPositions.remove(playerId);
    }
    @Override
    protected void executeAttack(PlayerEntity player) {
        // 瞬移到玩家面前
        this.teleport(player.getX(), player.getY(), player.getZ());
        // 获取 mob 伤害类型的注册表项
        RegistryEntry<DamageType> mobDamageType = this.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getEntry(RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("smfs", "ghost"))).orElseThrow(() -> new IllegalStateException("MOB_ATTACK damage type not found"));
        // 创建伤害源
        DamageSource damageSource = new DamageSource(mobDamageType, this);
        player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 20, 1));

        player.damage(damageSource, 100.0f);

    }
}